package com.ecmcloud.flow.node.impl;

import cn.hutool.core.collection.CollUtil;

import com.ecmcloud.common.constants.ProcessInstanceConstant;
import com.ecmcloud.common.dto.Result;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.common.dto.flow.NodeUser;
import com.ecmcloud.common.dto.third.DeptDto;
import com.ecmcloud.flow.node.AssignUserStrategy;
import com.ecmcloud.flow.utils.BizHttpUtil;
import com.google.common.collect.Lists;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 连续多级主管
 */
@Component
public class AssignUserLeaderTopStrategyImpl implements InitializingBean, AssignUserStrategy {
	
    @Override
    public List<String> handle(Node node, NodeUser rootUser, Map<String, Object> variables) {
        List<String> userIdList = Lists.newArrayList();
        //去获取主管
        Result<List<com.ecmcloud.common.dto.third.DeptDto>> r  = BizHttpUtil.queryParentDepListByUserId((rootUser.getId()));
        List<com.ecmcloud.common.dto.third.DeptDto> deptDtoList = r.getData();
        //上级主管依次审批
        //第几级主管审批截止
        Integer level = node.getDeptLeaderLevel();
        if (CollUtil.isNotEmpty(deptDtoList)) {
            int index = 1;
            for (DeptDto deptDto : deptDtoList) {
                if (level != null && level < index) {
                    break;
                }
                List<String> leaderUserIdList = deptDto.getLeaderUserIdList();
                if(CollUtil.isNotEmpty(leaderUserIdList)){
                    for (String s : leaderUserIdList) {
                        if(userIdList.contains(s)){
                            continue;
                        }
                        userIdList.add(s);
                    }
                }
                index++;
            }
        }
        return userIdList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(ProcessInstanceConstant.AssignedTypeClass.LEADER_TOP);
    }
}
